package com.adams.custom.page.mgt.dao.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.adams.access.db.bean.ViewPanelFilter;
import com.adams.access.db.dao.hibernate.HibernateGenericDao;
import com.adams.custom.page.util.ViewCacheUtil;

/**
 * 
 * File Name : ViewPanelFilterDaoImpl.java
 * 
 * @Description : 界面面板自定义过滤条件实体数据访问实现类
 * @author 904370
 */
public class ViewPanelFilterDaoImpl extends
		HibernateGenericDao<ViewPanelFilter, Long>
{

	private static String cacheMapByPk_key = "ViewPanelFilter_cacheMapByPk";

	private static String cacheMapByViewPanelid_key = "ViewPanelFilter_cacheMapByViewPanelid";

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.adams.access.db.dao.hibernate.HibernateGenericDao#save(java
	 * .lang.Object)
	 */
	public void save(ViewPanelFilter model)
	{
		super.save(model);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.adams.access.db.dao.GenericDao#get(java.io.Serializable)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public ViewPanelFilter get(Serializable id)
	{
		String key = id + "";
		Map cacheMapByPk = ViewCacheUtil.getConfigDataCacheMap(cacheMapByPk_key);
		if (!cacheMapByPk.containsKey(key))
		{
			synchronizationCache();
		}
		return (ViewPanelFilter) cacheMapByPk.get(key);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.adams.access.db.dao.hibernate.HibernateGenericDao#delete(java
	 * .lang.Object)
	 */
	public void delete(ViewPanelFilter model)
	{
		super.delete(model);
	}

	/**
	 * Description : 删除ViewItem对象列表
	 * 
	 * @param model
	 * 
	 */
	public void deleteAll(List<ViewPanelFilter> models)
	{
		for (ViewPanelFilter model : models)
		{
			super.delete(model);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.adams.access.db.dao.hibernate.HibernateGenericDao#saveOrUpdate
	 * (java.lang.Object)
	 */
	public void saveOrUpdate(ViewPanelFilter model)
	{
		super.saveOrUpdate(model);
	}

	/**
	 * Description : 根据界面面板得到ViewPanelFilter列表
	 * 
	 * @param viewpanelid
	 * @return
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List<ViewPanelFilter> findByViewPanelid(Long viewpanelid)
	{
		String key = viewpanelid + "";
		Map cacheMapByViewPanelid = ViewCacheUtil
				.getConfigDataCacheMap(cacheMapByViewPanelid_key);
		if (!cacheMapByViewPanelid.containsKey(key))
		{
			synchronizationCache();
		}
		List<ViewPanelFilter> result = new ArrayList<ViewPanelFilter>();
		List<ViewPanelFilter> temp = (List<ViewPanelFilter>) cacheMapByViewPanelid
				.get(key);
		if (null != temp && temp.size() > 0)
		{
			result.addAll(temp);
			// sortItemById(result);
		}
		return result;
	}

	private void sort(List<ViewPanelFilter> result)
	{
		if (null == result || result.size() == 0)
		{
			return;
		}
		Collections.sort(result, new Comparator<ViewPanelFilter>()
		{
			@Override
			public int compare(ViewPanelFilter o1, ViewPanelFilter o2)
			{
				return o1.getId().compareTo(o2.getId());
			}
		});
	}

	/**
	 * Description : 获得所有ViewItem对象
	 * 
	 * @return
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List<ViewPanelFilter> findAll()
	{
		List<ViewPanelFilter> result = null;
		Map cacheMapByPk = ViewCacheUtil.getConfigDataCacheMap(cacheMapByPk_key);
		if (cacheMapByPk.size() == 0)
		{
			result = synchronizationCache();
			return result;
		} else
		{
			result = new ArrayList<ViewPanelFilter>();
			Set keySet = cacheMapByPk.keySet();
			for (Iterator it = keySet.iterator(); it.hasNext();)
			{
				Object key = it.next();
				ViewPanelFilter temp = (ViewPanelFilter) cacheMapByPk.get(key);
				if (null != temp)
				{
					result.add(temp);
				}
			}
		}
		sort(result);
		return result;
	}

	private List<ViewPanelFilter> findAllFromDB()
	{
		List<ViewPanelFilter> result = query("from ViewPanelFilter");
		return result;
	}

	/**
	 * Description : 同步缓存
	 * 
	 * @return
	 * 
	 */
	@SuppressWarnings("unchecked")
	public synchronized List<ViewPanelFilter> synchronizationCache()
	{
		List<ViewPanelFilter> result = findAllFromDB();
		Map cacheMapByPk = ViewCacheUtil.getConfigDataCacheMap(cacheMapByPk_key);
		Map cacheMapByViewPanelid = ViewCacheUtil
				.getConfigDataCacheMap(cacheMapByViewPanelid_key);
		cacheMapByViewPanelid.clear();
		for (ViewPanelFilter viewPanelFilter : result)
		{
			cacheMapByPk.put(viewPanelFilter.getId() + "", viewPanelFilter);

			String viewpanelidStr = viewPanelFilter.getViewpanelid() + "";
			List<ViewPanelFilter> viewpanelList = null;
			if (cacheMapByViewPanelid.containsKey(viewpanelidStr))
			{
				viewpanelList = (List<ViewPanelFilter>) cacheMapByViewPanelid
						.get(viewpanelidStr);
			} else
			{
				viewpanelList = new ArrayList<ViewPanelFilter>();
			}
			viewpanelList.add(viewPanelFilter);
			cacheMapByViewPanelid.put(viewpanelidStr, viewpanelList);
		}

		// 排序处理
		Set<String> viewPanelids = cacheMapByViewPanelid.keySet();
		for (String viewPanelid : viewPanelids)
		{
			List<ViewPanelFilter> viewPanelFilters = (List<ViewPanelFilter>) cacheMapByViewPanelid
					.get(viewPanelid);
			sort(viewPanelFilters);
			cacheMapByViewPanelid.put(viewPanelid, viewPanelFilters);
		}

		ViewCacheUtil.put2ConfigDataCacheMap(cacheMapByPk_key, cacheMapByPk);
		ViewCacheUtil.put2ConfigDataCacheMap(cacheMapByViewPanelid_key,
				cacheMapByViewPanelid);
		return result;
	}
}
